#include "enemytank.h"

enemytank::enemytank()
{
srand((int)time(0));
}
int enemytank::touchpl()
{int size=39;int spe=speed;if(buff2){spe*=2;}
    int move=spe*(maps::mmap[(posy+19)/20][(posx+19)/20]+1);
    int min=move;
    switch(direction)
    {
    case 1:
        if(maps::myx>=posx-size&&maps::myx<=posx+size)
        {
           if((posy-move)<=(maps::myy+size)&&maps::myy+39<=posy)
           {
               if ((posy-(maps::myy+size))<min)
               {min=(posy-(maps::myy+size));};
           }
        }
        break;
    case 2:
        if(maps::myx>=posx-size&&maps::myx<=posx+size)
        {
           if((posy+size+move)>=maps::myy&&maps::myy>=(posy+size))
           {
               if( ((maps::myy)-posy-size)<min)
               {min=((maps::myy)-posy-size);};
           }
        }
        break;
    case 3:
        if(maps::myy>=posy-size&&maps::myy<=posy+size)
        {
           if((posx-move)<=maps::myx+size&&maps::myx+size<=posx)
           {
               if ((posx-(maps::myx)-size)<min)
               {
                   min=(posx-(maps::myx)-size);
               };
           }
        }
        break;
    case 4:
        if(maps::myy>=posy-size&&maps::myy<=posy+size)
        {
           if((posx+move+size)>=maps::myx&&maps::myx>=posx+size)
           {
               if (((maps::myx)-size-posx)<min)
               {
                   min=((maps::myx)-size-posx);
               };
           }
        }
        break;




    }
return min;}
int enemytank::is_touch()
{int size=39;int spe=speed;if(buff2){spe*=2;}
     int move=spe*(maps::mmap[(posy+19)/20][(posx+19)/20]+1);
     int min=move;

    for(int i=0;i<maps::enemynumber;i++)
    {if(i==num){continue;}
        switch(direction)
        {
        case 1:
            if(maps::enemyposx[i]>=posx-size&&maps::enemyposx[i]<=posx+size)
            {
               if((posy-move)<=(maps::enemyposy[i]+size)&&maps::enemyposy[i]+39<=posy)
               {
                   if ((posy-(maps::enemyposy[i]+size))<min)
                   {min=(posy-(maps::enemyposy[i]+size));};
               }
            }
            break;
        case 2:
            if(maps::enemyposx[i]>=posx-size&&maps::enemyposx[i]<=posx+size)
            {
               if((posy+size+move)>=maps::enemyposy[i]&&maps::enemyposy[i]>=(posy+size))
               {
                   if( ((maps::enemyposy[i])-posy-size)<min)
                   {min=((maps::enemyposy[i])-posy-size);};
               }
            }
            break;
        case 3:
            if(maps::enemyposy[i]>=posy-size&&maps::enemyposy[i]<=posy+size)
            {
               if((posx-move)<=maps::enemyposx[i]+size&&maps::enemyposx[i]+size<=posx)
               {
                   if ((posx-(maps::enemyposx[i])-size)<min)
                   {
                       min=(posx-(maps::enemyposx[i])-size);
                   };
               }
            }
            break;
        case 4:
            if(maps::enemyposy[i]>=posy-size&&maps::enemyposy[i]<=posy+size)
            {
               if((posx+move+size)>=maps::enemyposx[i]&&maps::enemyposx[i]>=posx+size)
               {
                   if (((maps::enemyposx[i])-size-posx)<min)
                   {
                       min=((maps::enemyposx[i])-size-posx);
                   };
               }
            }
            break;




        }
    }
    return min;
}
void enemytank::initenemy(int x,int y,int direct,int spe,int mod)
{
    posx=x;posy=y;direction=direct;speed=spe;mode=mod;is_player=!(mod+10);
    timer =new QTimer;
    timer->start(15);
    connect(timer,SIGNAL(timeout()),SLOT(enemy_update()));
    mybullet=new bullet;
    mybullet->setbullet(0,0,0,0);
};
void enemytank::enemy_update()
{if(is_life<=0){return;}
    int sp=speed;
    if(buff2){sp*=2;}

    if(maps::enemyposx[num]>530)
    {if(buff3<=0){
        is_life-=1;}
        if(is_life>0){
        maps::enemyposx[num]=posx;
        maps::enemyposy[num]=posy;
        }
    }
    posx=maps::enemyposx[num];
    posy=maps::enemyposy[num];

                int spe=sp*(maps::mmap[(posy+19)/20][(posx+19)/20]+1);
                int move=MIN(MIN(is_touch(),touchpl()),is_move());
    switch(mode)
    {//模式0，很呆
    case 0:{

        if(touchpl()<spe||is_move()<spe)
        {shoot();}
        tank_update();


        if(move==0)
        {
direction=int(rand()%4)+1;return;
        }
            switch(direction)
            {
            case 1:maps::enemyposy[num]-=move;break;
            case 2:maps::enemyposy[num]+=move;break;
            case 3:maps::enemyposx[num]-=move;break;
                case 4:maps::enemyposx[num]+=move;break;
            }
            if(turn==300)
            {
                direction=int(rand()%4)+1;turn=0;
            }
            turn++;
        break;
    }
    //模式2
    case 2:{


//移动
            switch(direction)
            {
            case 1:maps::enemyposy[num]-=move;break;
            case 2:maps::enemyposy[num]+=move;break;
            case 3:maps::enemyposx[num]-=move;break;
                case 4:maps::enemyposx[num]+=move;break;
            }
        tank_update();
        if(maps::playerhp<=0)
        {if(turn==0){
            direction=int(rand()%4)+1;turn=1;return;}return;}
        turn=0;
        if(touchpl()<spe||is_move()<spe)
        {shoot();}
            //改变方向
for(int i=0;i<1;i++){
                if(direction==1)
                {
                    if(maps::myy<posy&&move>0)
                    {break;}
                    if(maps::myy>posy)
                    {direction=2;break;}
                    if(move==0||maps::myy==posy)
                    {if(maps::myx>posx){direction=4;break;}else{direction=3;break;}}

                }
if(direction==2)
{
    if(maps::myy>posy&&move>0)
    {break;}

    if(maps::myy<posy){direction=1;break;}
    if(move==0||maps::myy==posy){if(maps::myx>posx){direction=4;break;}else{direction=3;break;}}
}
if(direction==3)
{if(maps::myx<posx&&move>0)
    {break;}
    if(maps::myx>posx){direction=4;break;}
    if(move==0||maps::myx==posx){if(maps::myy>posy){direction=2;break;}else{direction=1;break;}}
}
if(direction==4){if(maps::myx>posx&&move>0)
    {break;}if(maps::myx<posx){direction=3;break;}
if(move==0||maps::myx==posx){if(maps::myy>posy){direction=2;break;}else{direction=1;break;}}
}
}
//方向上有玩家就射击
switch(direction)
{
case 1:
    if(maps::myx+39>posx&&maps::myx<posx+39&&maps::myy<posy)
    {
        shoot();
    }
    break;
case 2:
{
    if(maps::myx+39>posx&&maps::myx<posx+39&&maps::myy>posy+39)
    {
        shoot();
    }
}
    break;
case 3:
    if(maps::myy+39>posy&&maps::myy<posy+39&&maps::myx<posx)
    {
        shoot();
    }
    break;
case 4:
    if(maps::myy+39>posy&&maps::myy<posy+39&&maps::myx>posx)
    {shoot();}
    break;

}
break;
    }
   //模式1正常
    case 1:{

        if(touchpl()<spe||is_move()<spe)
        {shoot();}
        tank_update();


        if(move==0)
        {
direction=int(rand()%4)+1;return;
        }
            switch(direction)
            {
            case 1:maps::enemyposy[num]-=move;break;
            case 2:maps::enemyposy[num]+=move;break;
            case 3:maps::enemyposx[num]-=move;break;
                case 4:maps::enemyposx[num]+=move;break;
            }
            if(turn==300)
            {
                direction=int(rand()%4)+1;turn=0;
            }
            turn++;
            switch(direction)
            {
            case 1:
                if(maps::myx+39>posx&&maps::myx<posx+39&&maps::myy<posy)
                {
                    shoot();
                }
                break;
            case 2:
            {
                if(maps::myx+39>posx&&maps::myx<posx+39&&maps::myy>posy+39)
                {
                    shoot();
                }
            }
                break;
            case 3:
                if(maps::myy+39>posy&&maps::myy<posy+39&&maps::myx<posx)
                {
                    shoot();
                }
                break;
            case 4:
                if(maps::myy+39>posy&&maps::myy<posy+39&&maps::myx>posx)
                {shoot();}
                break;

            }
        break;
    }
    }
};
